package com.fingard.dsp.bank.directbank.nbcb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class NBCB01BatPayRec extends NBCB01Base implements IBatPayRec {
    //1988
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    //9188
    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    //批量代收和代发工资提交
    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {

        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            serviceId = "srv010_batchTransfer";//代发代报销
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            if (!"".equals(bankFront.token)) {
                xmlWriter.writeElementString("functionId", "srv010");
                xmlWriter.writeElementString("functionName", "批量转账");
                xmlWriter.writeStartElement("opReq");
                xmlWriter.writeElementString("serialNo", p_reqBat.reqHeader.transSeqID);
                xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
                xmlWriter.writeStartElement("ReqParam");
                xmlWriter.writeElementString("transferDate", p_reqBat.payDate);//YYMMDD
                xmlWriter.writeElementString("totalAmt", p_reqBat.totalAmt);
                xmlWriter.writeElementString("totalNumber", p_reqBat.totalNum);
                xmlWriter.writeStartElement("list");
                while (p_reqBat.readRecord()) {
                    xmlWriter.writeStartElement("row");
                    xmlWriter.writeElementString("payAccount", p_reqBat.readedDetail.payAct);
                    xmlWriter.writeElementString("recAccount", p_reqBat.readedDetail.recAct);
                    xmlWriter.writeElementString("recAccountName", p_reqBat.readedDetail.recName);
                    xmlWriter.writeElementString("recBankName", p_reqBat.readedDetail.recBankName);
                    xmlWriter.writeElementString("province", p_reqBat.readedDetail.getOppProvinceAndCity()[0]);
                    xmlWriter.writeElementString("city", p_reqBat.readedDetail.getOppProvinceAndCity()[1]);
                    xmlWriter.writeElementString("transferAmt", p_reqBat.readedDetail.payAmount);
                    xmlWriter.writeElementString("currencyType", convertCurToBank(p_reqBat.readedDetail.payCur));
                    xmlWriter.writeElementString("transferType", "02");//大额（默认）
                    String submitType = p_reqBat.sameBnk.equals("1") ? "00" : p_reqBat.readedDetail.sameCity.equals("1") ? "01" : "02";
                    xmlWriter.writeElementString("submitType", submitType);
                    xmlWriter.writeElementString("remark", p_reqBat.readedDetail.abstractATS);
                    xmlWriter.writeElementString("recBankNo", p_reqBat.readedDetail.cnapsCode);
                    xmlWriter.writeEndElement();
                }
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpStatus = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                            String tmpInfo = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                            if (tmpStatus != null && tmpStatus.length() > 0) {
                                retAts.respCode = "0";
                                retAts.reqSeqID = p_reqBat.reqBatchNO;
                                retAts.respInfo = tmpInfo;
                                //90：交易成功91：交易失败94：银行已受理99：系统处理中
                                retAts.transState = tmpStatus + "+" + tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/orderState");
                                retAts.payInfoCode = retAts.transState;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpSbMsg.toString();
                        }
                    } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                        retAts.respCode = "0";
                        retAts.respInfo = tmpStrRet[1];
                        retAts.reqSeqID = p_reqBat.reqBatchNO;
                        retAts.transState = PaymentState.FAIL_Batch;
                        retAts.payInfoCode = tmpStrRet[0];
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpStrRet[1];
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSign[1];
                }
            }else{
                retAts.respCode = "-1";
                retAts.respInfo = "登录失败";
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    //1998 状态查询
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    //9198
    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }

    //批量代付和工资代付状态查询  银行不支持
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        return null;
    }
}
